package com.edee.bigday.coupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.edee.bigday.coupon.dao.SkuFullReductionDao;
import com.edee.bigday.coupon.entity.MemberPriceEntity;
import com.edee.bigday.coupon.entity.SkuFullReductionEntity;
import com.edee.bigday.coupon.entity.SkuLadderEntity;
import com.edee.bigday.coupon.service.MemberPriceService;
import com.edee.bigday.coupon.service.SkuFullReductionService;
import com.edee.bigday.coupon.service.SkuLadderService;
import com.edee.common.to.MemberPrice;
import com.edee.common.to.SkuReductionTo;
import com.edee.common.utils.PageUtils;
import com.edee.common.utils.Query;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    @Autowired
    SkuLadderService skuLadderService;
    @Autowired
    MemberPriceService memberPriceService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );
        return new PageUtils(page);
    }

    @Override
    public void saveSkuReduction(SkuReductionTo skuReductionTo) {
        //1、保存阶梯价格、满减折扣、会员价(bigday_sms.sms_sku_ladder、sms_sku_full_reduction、sms_member_price)
        //保存阶梯价格(sms_sku_ladder)：
        SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
        /*skuLadderEntity.setSkuId(skuReductionTo.getSkuId());
        skuLadderEntity.setFullCount(skuReductionTo.getFullCount());
        skuLadderEntity.setDiscount(skuReductionTo.getDiscount());
        skuLadderEntity.setAddOther(skuReductionTo.getCountStatus());*/
        //上面的set操作可以用属性对拷工具替代：
        BeanUtils.copyProperties(skuReductionTo, skuLadderEntity);
        //if(skuReductionTo.getFullCount()>0){
        if (skuLadderEntity.getFullCount() > 0) {
            //条件中的skuReductionTo.getFullCount()>0和skuLadderEntity.getFullCount()>0作用相同：
            skuLadderService.save(skuLadderEntity);
        }

        //2、保存满减折扣(sms_sku_full_reduction)：
        SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
        /*skuFullReductionEntity.setSkuId(skuReductionTo.getSkuId());
        skuFullReductionEntity.setFullPrice(skuReductionTo.getFullPrice());
        skuFullReductionEntity.setReducePrice(skuReductionTo.getReducePrice());
        skuFullReductionEntity.setAddOther(skuReductionTo.getCountStatus());*/
        //上面的set操作可以用属性对拷工具替代：
        BeanUtils.copyProperties(skuReductionTo, skuFullReductionEntity);
        if (skuFullReductionEntity.getFullPrice().compareTo(new BigDecimal("0")) == 1) {
            this.save(skuFullReductionEntity);
        }

        //3、保存会员价格(sms_member_price)：
        List<MemberPrice> memberPrices = skuReductionTo.getMemberPrice();
        List<MemberPriceEntity> collect = memberPrices.stream().map(memberPrice -> {
            MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
            //BeanUtils.copyProperties(price, memberPrice);
            //因为属性并未一一对应，所以不能使用属性对拷，需要注意set：
            memberPriceEntity.setSkuId(skuReductionTo.getSkuId());
            memberPriceEntity.setMemberLevelId(memberPrice.getId());
            memberPriceEntity.setMemberLevelName(memberPrice.getName());
            memberPriceEntity.setMemberPrice(memberPrice.getPrice());
            memberPriceEntity.setAddOther(1);
            return memberPriceEntity;
        }).filter(item->{
            return item.getMemberPrice().compareTo(new BigDecimal("0"))==1;
        }).collect(Collectors.toList());
        memberPriceService.saveBatch(collect);
    }

}